{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\AI\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From d:\\AI\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use the retry module or similar alternatives.\n"
     ]
    }
   ],
   "source": [
    "#from __future__ import absolute_import\n",
    "#from __futrue__ import division\n",
    "#from __futrue__ import print_function\n",
    "import argparse\n",
    "import sys\n",
    "import os\n",
    "import struct\n",
    "import numpy as np\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import tensorflow as tf\n",
    "FLAGS = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-d82eb5daac11>:2: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From d:\\AI\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From d:\\AI\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting F:\\ai\\week-6\\data\\compress\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From d:\\AI\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting F:\\ai\\week-6\\data\\compress\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From d:\\AI\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting F:\\ai\\week-6\\data\\compress\\t10k-images-idx3-ubyte.gz\n",
      "Extracting F:\\ai\\week-6\\data\\compress\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From d:\\AI\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "dir_minist_data = 'F:\\\\ai\\\\week-6\\\\data\\\\compress'\n",
    "mnist = input_data.read_data_sets(dir_minist_data, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\n# 增加隐层-1，配置3个神经元\\n# 增加隐层-2，配置2个神经元\\n# 测试发现效果还不如上面的拓扑，增加正则关系后效果更差！目前没有找到原因!\\n# create model\\nlearning_rate = 0.35\\nhidden1_units_num = 500\\nhidden2_units_num = 500\\n\\n# input\\nx = tf.placeholder(tf.float32, [None, 784], name = 'input')\\n\\nw11 = tf.Variable(tf.zeros([784, hidden1_units_num]))\\nb11 = tf.Variable(tf.zeros([hidden1_units_num]))\\nlogit11 = tf.matmul(x, w11) + b11\\nx11 = tf.nn.sigmoid(logit11)\\n\\nw12 = tf.Variable(tf.zeros([784, hidden1_units_num]))\\nb12 = tf.Variable(tf.zeros([hidden1_units_num]))\\nlogit12 = tf.matmul(x, w12) + b12\\nx12 = tf.nn.sigmoid(logit12)\\n\\nw21= tf.Variable(tf.zeros([hidden1_units_num, hidden2_units_num]))\\nb21 = tf.Variable(tf.zeros([hidden2_units_num]))\\nlogit21 = tf.matmul(x11, w21) + tf.matmul(x12, w21) + b21\\nx21 = tf.nn.sigmoid(logit21)\\n\\nw22= tf.Variable(tf.zeros([hidden1_units_num, hidden2_units_num]))\\nb22 = tf.Variable(tf.zeros([hidden2_units_num]))\\nlogit22 = tf.matmul(x11, w22) + tf.matmul(x12, w22) + b22\\nx22 = tf.nn.sigmoid(logit22)\\n\\nw23= tf.Variable(tf.zeros([hidden1_units_num, hidden2_units_num]))\\nb23 = tf.Variable(tf.zeros([hidden2_units_num]))\\nlogit23 = tf.matmul(x11, w23) + tf.matmul(x12, w23) + b23\\nx23 = tf.nn.sigmoid(logit23)\\n\\nw_output = tf.Variable(tf.zeros([hidden2_units_num, 10]))\\nb_output = tf.Variable(tf.zeros([10]))\\nlogit_output = tf.matmul(x21, w_output) + tf.matmul(x22, w_output)  + tf.matmul(x23, w_output) + b_output\\n\\n#regularizer = tf.contrib.layers.l1_regularizer(0.1, scope=None)\\n#tf.contrib.layers.apply_regularization(regularizer, weights_list=[w11, w12, w21, w22, w23, w_output])\\n\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 增加隐层-1，配置1个神经元，\n",
    "# create model\n",
    "learning_rate = 0.35\n",
    "hidden1_units_num = 365\n",
    "\n",
    "# input\n",
    "x = tf.placeholder(tf.float32, [None, 784], name = 'input')\n",
    "\n",
    "w1 = tf.Variable(tf.random_normal([784, hidden1_units_num]))\n",
    "b1 = tf.Variable(tf.random_normal([hidden1_units_num]))\n",
    "logit1 = tf.matmul(x, w1) + b1\n",
    "x1 = tf.nn.sigmoid(logit1)\n",
    "\n",
    "# set hidden-2's node is hidden2_units_num, use sigmoid as activation-function in hidden-1\n",
    "#w2 = tf.Variable(tf.random_normal([hidden1_units_num, hidden2_units_num]))\n",
    "#b2 = tf.Variable(tf.random_normal([hidden2_units_num]))\n",
    "#logit2 = tf.matmul(x1, w2) + b2\n",
    "#x2 = tf.nn.sigmoid(logit2)\n",
    "\n",
    "# output\n",
    "w_output = tf.Variable(tf.zeros([hidden1_units_num, 10]))\n",
    "b_output = tf.Variable(tf.zeros([10]))\n",
    "logit_output = tf.matmul(x1, w_output) + b_output\n",
    "\n",
    "'''\n",
    "# 增加隐层-1，配置3个神经元\n",
    "# 增加隐层-2，配置2个神经元\n",
    "# 测试发现效果还不如上面的拓扑，增加正则关系后效果更差！目前没有找到原因!\n",
    "# create model\n",
    "learning_rate = 0.35\n",
    "hidden1_units_num = 500\n",
    "hidden2_units_num = 500\n",
    "\n",
    "# input\n",
    "x = tf.placeholder(tf.float32, [None, 784], name = 'input')\n",
    "\n",
    "w11 = tf.Variable(tf.zeros([784, hidden1_units_num]))\n",
    "b11 = tf.Variable(tf.zeros([hidden1_units_num]))\n",
    "logit11 = tf.matmul(x, w11) + b11\n",
    "x11 = tf.nn.sigmoid(logit11)\n",
    "\n",
    "w12 = tf.Variable(tf.zeros([784, hidden1_units_num]))\n",
    "b12 = tf.Variable(tf.zeros([hidden1_units_num]))\n",
    "logit12 = tf.matmul(x, w12) + b12\n",
    "x12 = tf.nn.sigmoid(logit12)\n",
    "\n",
    "w21= tf.Variable(tf.zeros([hidden1_units_num, hidden2_units_num]))\n",
    "b21 = tf.Variable(tf.zeros([hidden2_units_num]))\n",
    "logit21 = tf.matmul(x11, w21) + tf.matmul(x12, w21) + b21\n",
    "x21 = tf.nn.sigmoid(logit21)\n",
    "\n",
    "w22= tf.Variable(tf.zeros([hidden1_units_num, hidden2_units_num]))\n",
    "b22 = tf.Variable(tf.zeros([hidden2_units_num]))\n",
    "logit22 = tf.matmul(x11, w22) + tf.matmul(x12, w22) + b22\n",
    "x22 = tf.nn.sigmoid(logit22)\n",
    "\n",
    "w23= tf.Variable(tf.zeros([hidden1_units_num, hidden2_units_num]))\n",
    "b23 = tf.Variable(tf.zeros([hidden2_units_num]))\n",
    "logit23 = tf.matmul(x11, w23) + tf.matmul(x12, w23) + b23\n",
    "x23 = tf.nn.sigmoid(logit23)\n",
    "\n",
    "w_output = tf.Variable(tf.zeros([hidden2_units_num, 10]))\n",
    "b_output = tf.Variable(tf.zeros([10]))\n",
    "logit_output = tf.matmul(x21, w_output) + tf.matmul(x22, w_output)  + tf.matmul(x23, w_output) + b_output\n",
    "\n",
    "#regularizer = tf.contrib.layers.l1_regularizer(0.1, scope=None)\n",
    "#tf.contrib.layers.apply_regularization(regularizer, weights_list=[w11, w12, w21, w22, w23, w_output])\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-4-987b5526748a>:2: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "y_ = tf.placeholder(tf.float32, [None, 10], name = 'output')\n",
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=logit_output))\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "sess = tf.Session()\n",
    "sess.run(init)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(2000000):\n",
    "    #print('[+] train times-%s start' % _)\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x: batch_xs, y_ : batch_ys})\n",
    "    #print('[+] train times-%s end' % _)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.963700\n"
     ]
    }
   ],
   "source": [
    "correct_prediction = tf.equal(tf.argmax(logit_output, 1), tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "print('%f' % sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
